import { Request, Response } from 'express'
import fs from 'fs';

const jsonStr = fs.readFileSync('./data.json', {
    encoding: 'utf8'
})

const datalist = JSON.parse(jsonStr)

const data = (req: Request, res: Response) => {
    res.send(datalist)
}

const home = (req: Request, res: Response) => {
    res.send('Hello world')
}

const login = (req: Request, res: Response) => {
    if (req.query.user == 'admin' && req.query.password == '123456') {
        res.json({
            code: 200,
            message: 'admin身份登录成功',
            route: [
                {
                    path: '/demo1',
                    name: 'Demo1',
                    component: 'demo1'
                },
                {
                    path: '/demo2',
                    name: 'Demo2',
                    component: 'demo2'
                },
                {
                    path: '/demo3',
                    name: 'Demo3',
                    component: 'demo3'
                }
            ]
        })
    } else if (req.query.user == 'xiaoguo' && req.query.password == '123456') {
        res.json({
            code: 200,
            message: 'admin身份登录成功',
            route: [
                {
                    path: '/demo1',
                    name: 'Demo1',
                    component: 'demo1'
                },
                {
                    path: '/demo2',
                    name: 'Demo2',
                    component: 'demo2'
                }
            ]
        })
    }
    else {
        res.json({
            code: 400,
            message: '账号密码错误'
        })
    }
}


export {
    home, data, login
}